<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 11:31
 */
namespace app\api\controller\v1;

use app\api\validate\Count;
use app\api\validate\IDMustPositveInt;
use app\lib\exception\ProductException;
use think\Controller;
use app\api\model\Product as ProductModel;

class Product extends Controller {

    /**获取最新商品信息
     * @param int $count
     *
     * @return \think\response\Json
     * @throws ProductException
     * @throws \app\lib\exception\ParemeterException
     */
    public function getNewProduct($count = 5){
        (new Count())->goCheck();
        $product = ProductModel::getMostNewProducts($count);
//        $collection = collection($product);
        if(!$product){
            throw new ProductException();
        }
//        $product = $collection->hidden(['summary']);
        return json(['code'=>200,'msg'=>'OK','data'=>$product]);
    }

    /**
     * 获取指定类目下面的所有的商品信息
     * @param $id     *
     * @return \think\response\Json
     * @throws ProductException
     * @throws \app\lib\exception\ParemeterException
     */
    public function getAllInCategory($id){
        (new IDMustPositveInt())->goCheck();
        $cateProduct = ProductModel::getCateProductsByID($id);
        if(!$cateProduct){
            throw new ProductException();
        }
        return json(['code'=>200,'msg'=>'OK','data'=>$cateProduct]);
    }

    /**
     * 获取商品详情
     * @param $id
     *
     * @return \think\response\Json
     * @throws \app\lib\exception\ParemeterException
     */
    public function getProductInfo($id){
        (new IDMustPositveInt())->goCheck();
        $productInfo = ProductModel::getProductInfoByID($id);
        if(!$productInfo){
            throw new ProductException();
        }
        return json(['code'=>200,'msg'=>'OK','data'=>$productInfo]);
    }


    public function deleteProduct($id){

    }

}







